﻿using System;
using Microsoft.Xna.Framework;
using RaptorEngine;
using OilRigSumoWrestlers.Controllables.Controllers;
using RaptorEngine.Components;
using OilRigSumoWrestlers.Levels;

namespace OilRigSumoWrestlers.Artificial_intelligence
{
    /// <summary>
    /// Extend this class to create new behaviors for the AI
    /// </summary>
    public abstract class Behavior
    {
        protected Entity agent, enemy;
        protected PlayerControllable agentControllable;
        public bool ApproachingEnemy { get; set; }
        public float DistanceToEnemy { get; set; }
        protected Movable enemyMovable;
        protected Movable agentMovable;
        protected ALevel level;

        protected Behavior(Entity agent, Entity enemy, PlayerControllable controllable, ALevel level)
        {
            this.level = level;
            this.agent = agent;
            this.enemy = enemy;
            agentControllable = controllable;
            if (enemy.HasComponent(ComponentType.Movable))
                enemyMovable = (Movable)enemy.GetComponent(ComponentType.Movable);
            if (agent.HasComponent(ComponentType.Movable))
                agentMovable = (Movable)agent.GetComponent(ComponentType.Movable);

        }

        public abstract void Update(GameTime gameTime);

        /// <summary>
        /// Returns the direction to go to avoid falling of edges and running into obstacles
        /// </summary>
        protected Vector2 GetAvoidDirection(GameTime gameTime)
        {
            Vector3 direction;
            //if standing on a dangerous tile, move away from it else move in the opposite direction of your velocity
            if (!AIHelper.IsMovableTile(level.GetIndexAtPosition(agent.Position), level))
            {
                direction = agent.Position - level.GetPositionForTile(level.GetIndexAtPosition(agent.Position));
            }
            else
                direction = Vector3.Negate(agentMovable.Velocity);

            direction.Normalize();
            return new Vector2(direction.X, direction.Z);
        }
    }
}
